//测试案例
function test(str){//渲染一个列表
	temp.renderList({
		 elem: str, //元素定位标识
		 auto:true, //自动匹配页面上的{{filed}}替换成数据
		 data: [{"name":"aaa","age":12},{"name":"bbb","age":15}],//需要渲染到页面的数据
		 url:"",//从网络获取数据
		 param:{},//请求参数
		 key:"list",//返回List数据所对应的key 如果返回的是一个数组则不填
		 toHtml:function(d){//手动渲染 auto：false 时生效，页面上生成return 的内容，d为list中的数据
			 return "当前名称："+d.name+"--当前年龄："+d.age+"";
		 }
		
	})
}

function test2(str){//渲染普通页面
	temp.renderHtml({
		 elem: str,
		 data: {"name":"aaa","age":12}
	})
}

//------------------------模板引擎--------------------

function temp(){}
temp.temps = [];

temp.renderList = function(obj){
	temp.render(obj, temp.setHList);
}

temp.renderHtml = function(obj){
	temp.render(obj, temp.setHtml);
}

temp.renderClassify = function(obj){//按照类别区分
	var list = obj.data;
	var classid = obj.classid;
	var pelem = obj.pelem;
	var elem = obj.elem;
	var handle = obj.handle;
	var success = obj.success;
	var ls = {};
	
	for(var i in list){
		var d = list[i];
		if(ls[d[classid]]==null){
			ls[d[classid]] = [d];
		}else{
			ls[d[classid]].push(d);
		}
	}
	
	var parents = [];
	for(var i in ls){
		var d = ls[i][0];
		parents.push(obj.parent(d));
	}
	
	temp.renderList({
		elem:pelem,
		data:parents,
		success:function(){
			for(var i in ls){
				temp.renderList({
					elem:elem.replace("?",i),
					data:ls[i],handle,success
				})
			}
		}
	})
	
}




temp.render=function(obj,rende){
	var data = obj.data;	
	var key = obj.key;
	var elem = obj.elem;
	if(temp.temps[elem]==null){
		var h = $(elem).find("tempList").html();
		temp.temps[elem] = h;
		//$(elem).html(h.replace(/{{.*?}}/g,""))
	}
	
	if(data==null||data==""){
		var url = obj.url;
		if(url!=null&&url!=""){
			$.post(url,obj.param,function(data){
				if(key==null||key==""){
					rende(obj,data);
				}else{
					rende(obj,data[key]);
				}
			},"JSON");			
		}else{
			if(obj.fail!=null)obj.fail("data 和 url 参数不可同时为空");
		}
	}else{
		rende(obj,data);
	}
}

//渲染普通页面
temp.setHtml=function(obj,data){
	var elem = obj.elem;
	var auto = obj.auto==null?true:obj.auto;//默认自动匹配
	var html="";
	if(data!=null&&!Array.isArray(data)){
		if(auto){
			var h = temp.temps[elem];//$(elem).html();
			if(obj.handle!=null){//处理数据
				data = obj.handle(data);
			}
			html+=temp.toHtml(h, data)
			
		}else{
			html+=obj.toHtml(data);		
		}
		$(elem).html(html);
	
		
		if(obj.success!=null)obj.success(data);
	}else{
		if(obj.fail!=null)obj.fail("data不可为数组");
		//console.error()
	}
}

//渲染列表
temp.setHList=function(obj,data){
	var elem = obj.elem;
	var auto = obj.auto==null?true:obj.auto;//默认自动匹配
	var html="";
	if(data!=null&&Array.isArray(data)){
		if(auto){
			var h = temp.temps[elem];//$(elem).html();
			for(var i in data){
				var d = data[i];
				if(obj.handle!=null){//处理数据
					d = obj.handle(d);
				}
				html+=temp.toHtml(h, d);
			}
		}else{
			for(var i in data){
				var d = data[i]
				html+=obj.toHtml(d,i);
			}				
		}
		$(elem).html(html);
		if(obj.success!=null)obj.success(data);
	}else{
		if(obj.fail!=null)obj.fail("data必须为数组");
		//console.error("data必须为数组")
	}
}


/**将数据填入模板内生成HTML页面*/
temp.toHtml=function(html,data){
	for(var k in data){
		html = html.replace(new RegExp( "{{"+k+"}}", "g"),data[k])
	}
	return html;
}

//------------------------模板引擎--------------------

//------------------------表单操作--------------------


function forms(){}
//根据H5的pattern和required
forms.submit = function(obj){
	var elem = obj.elem;
	var url = obj.url;
	var auto = url==null?false:true;//如果url不等于空默认自动提交
	var param = {};
	var forms = $(elem).find("input,textarea,select [name!=null]");
	for(var i in forms){
		var d = forms.eq(i);
		var name = d.attr("name");
		var value = d.val();
		var title = d.attr("title");
		var pattern = d.attr("pattern");
		var required = d.attr("required")!=undefined;
		
		if(value!=null&&value!=""){
			if(pattern!=null&&!new RegExp(pattern).test(value)){
				if(obj.fail!=null)obj.fail(title,d);
				return null;
			}else{
				param[name] = value;
			}
		}else{
			if(required){
				if(obj.fail!=null)obj.fail(title,d);
				return null;
			}
		}
	}
	if(auto){
		$.ajax({
            type: "POST",
            url: url,
            data: param,
            dataType: "json",
            success: function(data){
            	if(obj.success!=null){
    				obj.success(data,forms);
    			}else{
    				console.error("自动提交模式下success方法不能为空")				
    			}
            },error:function(error){
            	console.error("请求失败:"+error)
            }
        });
		
	}else{
		if(obj.commit!=null){
			obj.commit(param,forms);
		}else{
			console.error("手动提交模式下commit方法不能为空")
		}
	}
	
	return param;
}

//添加表单验证
forms.addVale = function(elem,list){
	var f = $(elem);
	for(var i in list){
		var d = list[i];
		var input = f.find("[name='"+d.name+"']");
		input.attr("title",d.title);
		input.attr("pattern",d.pattern);
		input.val(d.value);
		if(d.required){
			input.attr("required","required");
		}
	}
}

//表单数据回显
forms.render = function(obj) {
	var elem = $(obj.elem);
	var url = obj.url;
	var param = obj.param;
	
	$.post(url,param,function(data){ 
		var d = obj.exec(data);
		if(d!=null){
			for(var i in d){
				elem.find("[name='"+i.toLowerCase()+"']").val(d[i]);
			}
		}
	},"JSON");
}





//------------------------表单操作--------------------


